﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using NVelocity.App;
using NVelocity;
using Commons.Collections;
using NVelocity.Runtime;
using System.Xml;

namespace DalGen.Service
{
    public abstract class BuilderBase
    {
		public VelocityEngine velocity;
		public VelocityContext context;
        public XmlDocument domSql2DotNet;
		public BuilderBase()
		{
			InitVelocity();
            InitXml();
		}

		public  virtual void InitVelocity()
		{
			//创建VelocityEngine实例对象
			velocity = new VelocityEngine();
			context = new VelocityContext();
			ExtendedProperties props = new ExtendedProperties();
			//使用设置初始化VelocityEngine
			props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file");
			props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, GetCurrentExecutePath());
            props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, true);
			velocity.Init(props);
			//为模板变量赋值
		}


		protected virtual string GetLanguageTypeName(string sSqlType)
		{
			
			XmlNode xmlNode = domSql2DotNet.SelectSingleNode("DataBase/" + sSqlType);

			//获取节点的属性，并循环取出需要的属性值
			XmlAttributeCollection xmlAttr = xmlNode.Attributes;
			string strReturn = "";
			for (int i = 0; i < xmlAttr.Count; i++)
			{
				if (xmlAttr.Item(i).Name == "langType")
				{
					strReturn = xmlAttr.Item(i).Value;
					break;
				}
			}
			return strReturn;
		}

        protected virtual string GetDbTypeName(string sSqlType)
        {
            
            XmlNode xmlNode = domSql2DotNet.SelectSingleNode("DataBase/" + sSqlType);

            //获取节点的属性，并循环取出需要的属性值
            XmlAttributeCollection xmlAttr = xmlNode.Attributes;
            string strReturn = "";
            for (int i = 0; i < xmlAttr.Count; i++)
            {
                if (xmlAttr.Item(i).Name == "dbType")
                {
                    strReturn = xmlAttr.Item(i).Value;
                    break;
                }
            }
            return strReturn;
        }


        private void InitXml()
        {
            string sSplit = System.AppDomain.CurrentDomain.BaseDirectory.Substring(System.AppDomain.CurrentDomain.BaseDirectory.Length - 1);
			string ConfigPath;
			if (sSplit != @"\" && sSplit != "/")
				ConfigPath = System.AppDomain.CurrentDomain.BaseDirectory + @"\";
			else
				ConfigPath = System.AppDomain.CurrentDomain.BaseDirectory;
			string sConfigFileName = ConfigPath + @"Reflection\SQL2DotNet.xml";
			domSql2DotNet = new XmlDocument();
			domSql2DotNet.Load(sConfigFileName);
        }

        /// <summary>
        /// 获取当前可执行文件所在路径
        /// </summary>
        /// <returns>当前可执行文件所在路径</returns>
        protected string GetCurrentExecutePath()
        {
            string sSplit = System.AppDomain.CurrentDomain.BaseDirectory.Substring(System.AppDomain.CurrentDomain.BaseDirectory.Length - 1);
            string sPath;
            if (sSplit != @"\" && sSplit != "/")
                sPath = System.AppDomain.CurrentDomain.BaseDirectory + @"\";
            else
                sPath = System.AppDomain.CurrentDomain.BaseDirectory;

            return sPath;
        }
    }

}
